﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace Hi_Baidu
{
    /// <summary>
    /// Win_ChanageHeadImage.xaml 的交互逻辑
    /// </summary>
    public partial class Win_ChanageHeadImage : Window, IMetaData
    {
        private String SELECT_IMAGE_FLAG;
        private Win_ModifyUserInfo win_ModifyUserInfo;

        /// <summary>
        ///     更换头像窗口
        /// </summary>
        public Win_ChanageHeadImage()
        {
            InitializeComponent();
        }

        /// <summary>
        ///     更换头像窗口
        /// </summary>
        /// <param name="Key" type="string">
        ///     <para>
        ///         窗口唯一标示
        ///     </para>
        /// </param>
        /// <param name="win_ModifyUserInfo" type="Hi_Baidu.Win_ModifyUserInfo">
        ///     <para>
        ///         更新用户资料窗口句柄
        ///     </para>
        /// </param>
        /// <param name="SELECT_IMAGE_FLAG" type="string">
        ///     <para>
        ///         当前用户头像标示
        ///     </para>
        /// </param>
        public Win_ChanageHeadImage(String Key, Win_ModifyUserInfo win_ModifyUserInfo, String SELECT_IMAGE_FLAG)
        {
            this.key = Key;
            this.SELECT_IMAGE_FLAG = SELECT_IMAGE_FLAG;
            this.win_ModifyUserInfo = win_ModifyUserInfo;
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.lst_UserHeadReView.HorizontalAlignment = HorizontalAlignment.Left;
            this.lst_UserHeadReView.HorizontalContentAlignment = HorizontalAlignment.Left;
            XDocument adList = XDocument.Load("../syshead/UserSysHead.xml");
            var CM_FRIEND_TREEVIEW = from cm_FRIEND_TREEVIEW
                                         in adList.Descendants("head_set").Elements("head")
                                     select cm_FRIEND_TREEVIEW;
            //开始头像菜单
            foreach (var item in CM_FRIEND_TREEVIEW)
            {
                ListBoxItem listBoxItem = new ListBoxItem();
                listBoxItem.Tag = item.Attribute("md5").Value + "." + item.Attribute("type").Value;
                listBoxItem.Margin = new Thickness(0, 5, 0, 5);
                listBoxItem.HorizontalAlignment = HorizontalAlignment.Left;
                listBoxItem.HorizontalContentAlignment = HorizontalAlignment.Left;
                Grid grid = new Grid();
                grid.Width = 400;
                grid.HorizontalAlignment = HorizontalAlignment.Left;
                listBoxItem.Content = grid;
                //2 x 2的表格
                grid.RowDefinitions.Add(new RowDefinition());
                grid.RowDefinitions.Add(new RowDefinition());
                grid.ColumnDefinitions.Add(new ColumnDefinition());
                grid.ColumnDefinitions.Add(new ColumnDefinition());
                grid.ColumnDefinitions[0].Width = GridLength.Auto;
                //图片和外边框
                Border Out_Boder = new Border();
                Out_Boder.Width = 100;
                Out_Boder.Height = 100;
                Out_Boder.HorizontalAlignment = HorizontalAlignment.Left;
                Out_Boder.BorderBrush = Brushes.SkyBlue;
                Out_Boder.BorderThickness = new Thickness(3);
                Out_Boder.CornerRadius = new CornerRadius(5, 5, 5, 5);
                grid.Children.Add(Out_Boder);
                Grid.SetRow(Out_Boder, 0);
                Grid.SetColumn(Out_Boder, 0);
                Grid.SetRowSpan(Out_Boder, 2);
                //头像预览图
                Image img = new Image();
                img.Source =new BitmapImage
                                     (
                                         new Uri(
                                             "pack://application:,,,/syshead/" + item.Attribute("md5").Value + "." + item.Attribute("type").Value
                                                     )
                                     );
                img.Width = 100;
                img.Height = 100;
                Out_Boder.Child = img;
                //头像标签名
                TextBlock tb_ImageName = new TextBlock();
                tb_ImageName.HorizontalAlignment = HorizontalAlignment.Left;
                tb_ImageName.Margin = new Thickness(10);
                tb_ImageName.Text = item.Attribute("name").Value;
                grid.Children.Add(tb_ImageName);
                Grid.SetRow(tb_ImageName,0);
                Grid.SetColumn(tb_ImageName,1);
                //头像说明
                TextBlock tb_Commond = new TextBlock();
                tb_Commond.HorizontalAlignment = HorizontalAlignment.Left;
                tb_Commond.Foreground = Brushes.LightGray;
                tb_Commond.Margin = new Thickness(10,0,10,10);
                tb_Commond.Text = item.Attribute("commond").Value;
                grid.Children.Add(tb_Commond);
                Grid.SetRow(tb_Commond,1);
                Grid.SetColumn(tb_Commond,1);
                //创建 ”已选择“ 标示 
                if (item.Attribute("md5").Value == SELECT_IMAGE_FLAG)
                {
                    Image img_Selected = new Image();
                    img_Selected.Width = 97;
                    img_Selected.Height = 59;
                    img_Selected.HorizontalAlignment = HorizontalAlignment.Right;
                    img_Selected.VerticalAlignment = VerticalAlignment.Center;
                    img_Selected.Source = new BitmapImage(new Uri("pack://application:,,,/SysImage/Selected.png"));
                    grid.Children.Add(img_Selected);
                    Grid.SetRow(img_Selected, 0);
                    Grid.SetColumn(img_Selected, 1);
                    Grid.SetRowSpan(img_Selected, 2);
                    listBoxItem.IsSelected = true;
                }
                this.lst_UserHeadReView.Items.Add(listBoxItem);
            }
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                this.DragMove();
            }
        }

        #region IMetaData 成员
        private String key;
        public string Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
            }
        }

        #endregion

        private void btn_Cancle_Click(object sender, RoutedEventArgs e)
        {
            Globle.popWindowManager.Dispose(this.key);
            this.Close();
        }

        private void btn_OK_Click(object sender, RoutedEventArgs e)
        {
            Globle.popWindowManager.Dispose(this.key);
            //分割选中图片的信息
            String[] SelectImageInfo = ((ListBoxItem)this.lst_UserHeadReView.SelectedItem).Tag.ToString().Split(new String[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            this.win_ModifyUserInfo.ChanageHeadImage
                (
                    this.SELECT_IMAGE_FLAG,
                    SelectImageInfo[0],
                    SelectImageInfo[1]
                );
            this.Close();
        }
    }
}
